<template>
  <div class="cesium-viewer-wapper">
    <div id="cesium-viewer" />
    <viewerSettingSidebar :viewerSource="viewerSource" />
    <MousePosition show :viewerSource="viewerSource" />
    <g3vPlayer showTime :clockOptions="clockOptions" :viewerSource="viewerSource" :sliderOption="sliderOption" v-model:playing="playing" />
  </div>
</template>

<script>
import * as Cesium from '@cesiumjs';
import { defineComponent, ref, watch } from 'vue';
import { SelectBox, SliderBar } from '@components/widgets';
import { G3V } from '@g3x';
import { MousePosition, viewerSettingSidebar, g3vPlayer } from '@g3x/g3v/ui';
import { g3a_groundImages } from '@g3x/g3a';

const g3vOptions = {
  defaultViewRectangle: G3V.defaultViews.global.china.rectangle,
  viewers:[{
    dom: "cesium-viewer",
    name: "viewer1",
    title: "viewer#01",
    viewer:{
      imagery: { type: 'arcgisImagery', options: { brightness: 1, show: true } },
      imageryProvider: null,
      terrain: { type: 'default', options: { requestVertexNormals: true, requestWaterMask: true } },
      terrainProvider: null,
      shadows: false,
    },
    layers:[
      // { type: 'tdtIboMap', options: { brightness: 1, show: true } },
      // { type: 'tdtImgAnno', options: { brightness: 1, show: true } },
      // { type: 'darkMask', options: { brightness: 1, alpha: 0.85 } },
      { type: 'graticule', options: { brightness: 1, show: true } }
    ],
    globe: {
      depthTestAgainstTerrain: false,
      // enableLighting: true //启用以太阳为光源的地球
    },
    navigation: {
      // defaultResetView: G3V.defaultViews.global.china.rectangle ,
      defaultResetView: null //flyHome
    },
    hideLayers: false,
  }],
};

export default defineComponent({
  name: 'groundImages', // 热力图
  components: { SelectBox, SliderBar, viewerSettingSidebar, g3vPlayer, MousePosition },
  setup() {
    return { groundImages: null };
  },
  props: {},
  data() {
    return {
      playing: true,
      g3v: null,
      timeStart: '2021-05-03T00:00:00+08:00',
      timeEnd: '2021-05-09T00:00:00+08:00',
      sliderOption: { min: 0, max: 3600 * 24, step: 100 }
    };
  },
  computed: {
    g3vViewer() {
      return this.g3v && this.g3v.g3vViewer$1;
    },
    viewer() {
      return this.g3v && this.g3v.viewer$1;
    },
    viewerSource() {
      const { viewer } = this;
      return { viewer };
    },
    clockOptions() {
      const { timeStart, timeEnd } = this;
      const startTime = Cesium.JulianDate.fromIso8601(timeStart);
      const stopTime = Cesium.JulianDate.fromIso8601(timeEnd);
      const clockRange = Cesium.ClockRange.LOOP_STOP;
      return { multiplier: 3600 * 4, startTime, stopTime, clockRange };
    }
  },
  watch: {},
  mounted() {
    console.log('groundImages mounted');
    this.initViewer();
  },
  beforeUnmount() {},
  unmounted() {
    this.groundImages && this.groundImages.destroy();
    this.groundImages = null;
    this.g3v && this.g3v.destroy();
    this.g3v = null;
  },
  methods: {
    initViewer() {
      let vm = this;
      vm.g3v = new G3V(g3vOptions,()=>{
        vm.onViewerInited();
        vm.initEffects();
      });
    },
    onViewerInited() {
      let vm = this;
      const lyrDefine = {
        type: 'tileset',
        url: '//zzgis.com/cdn/3d/SampleData/earthSDK/dayanta/tileset.json',
        options: { name: 'dayanta', title: 'dayanta', brightness: 1, show: true }
      };

      vm.g3vViewer.addTileset(lyrDefine, (tileset,viewer) => {
        const hpr = new Cesium.HeadingPitchRange(0.0, -0.5, tileset.boundingSphere.radius * 2.0)
        viewer.zoomTo(tileset, hpr);
        vm.setHeight = function(height) {
          height = Number(height);
          if (isNaN(height)) {
            return;
          }
          var cartographic = Cesium.Cartographic.fromCartesian(tileset.boundingSphere.center);
          var surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, 0.0);
          var offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, height);
          var translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3());
          tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
        };
        vm.setHeight(0)
      });
    },
    initEffects() {
      let vm = this;
      const viewer = this.viewer;
      var scene = viewer.scene;

      const dataSources = [
        {time: '2021-05-04T00:00:00+08:00', url:'baidu.png'},
        {time: '2021-05-05T00:00:00+08:00', url:'logo.png'},
        {time: '2021-05-06T00:00:00+08:00', url:'sougou.jpg'},
        {time: '2021-05-07T00:00:00+08:00', url:'fire.png'},
        {time: '2021-05-08T00:00:00+08:00', url:'cameraVideoMask.png'},
      ];
      dataSources.forEach(e=>{
        e.url = `//zzgis.com/cdn/3d/SampleData/earthSDK/images/${e.url}`
      });

      // const urls = ['baidu.png', 'logo.png', 'sougou.jpg', 'fire.png', 'cameraVideoMask.png'].map(e => `//zzgis.com/cdn/3d/SampleData/earthSDK/images/${e}`);

      const timeStart = '2021-05-03T12:00:00+08:00';
      const timeEnd = '2021-05-08T12:00:00+08:00';
      vm.timeStart = timeStart;
      vm.timeEnd = timeEnd;
      const [textureWidth, textureHeight] = [1280, 1280];
      const origin = [0.5, 0.5];
      const position = [1.9017007449558452, 0.5972456941664638, 1525.1468750219];
      const config = { position, origin, textureWidth, textureHeight, timeStart, timeEnd }
      vm.groundImages = new g3a_groundImages(viewer, config, dataSources);
      vm.groundImages.flyTo({
        complete: () => {
          vm.playing = true;
        }
      });
    }
  }
});
</script>

<style lang="less"></style>
